﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using testy;

namespace pytator {
	public partial class QuestionsControl : UserControl {
		public delegate void del (int i);
		public delegate void del2 (int i, List<bool> ans);
		public delegate void del3();
		public event del questionSelected;
		public event del2 answersUpdate;
		public event del3 testEnd;
		private Label timerLabel;
		private Timer timer;
		private int maxTime;
		private int actualTime;
		private int questionNumber;
		public QuestionsControl() {
			InitializeComponent();
		}
		private void questionControl1_Load(object sender, EventArgs e) {

		}
		public void loadQuestion(CheckedQuestion q) {//załadowanie pytania
			this.questionControl1.loadQuestion(q);
		}
		public void loadQuestionsList(int i) { //załadowanie listy pytań
			this.questionsListBox.Items.Clear();
			for(int k=0; k<i; k++) {
				this.questionsListBox.Items.Add("Pytanie"+k);
			}
		}
		private void questionsListBox_SelectedIndexChanged(object sender, EventArgs e) { //obsługa zmiany pytania
			answersUpdate(questionNumber, this.questionControl1.getAnswers());
			questionSelected(questionsListBox.SelectedIndex);
			this.questionNumber = questionsListBox.SelectedIndex;
		}

		private void koniecButton_Click(object sender, EventArgs e) { //obsługa kliknięcia przycisku kończącego test
			this.questionsListBox.SelectedIndex = 0;
			answersUpdate(questionNumber, this.questionControl1.getAnswers());
			testEnd();
		}
		public void loadTimer(int t) { //załadowanie licznika
			this.maxTime = t;
			this.timer = new Timer();
			this.timer.Interval = 1000;
			this.timer.Tick += timer_Tick;
			this.timerLabel = new Label();
			this.timerLabel.Location = new Point(170, 118);
			this.timerLabel.Anchor = ((AnchorStyles)(AnchorStyles.Right|AnchorStyles.Bottom));
			this.Controls.Add(this.timerLabel);
			this.timer.Start();
		}
		private void timer_Tick(object sender, EventArgs e) { //aktualizacja czasu i ew. zakończenie testu po przekroczeniu czasu
			this.actualTime++;
			if(this.maxTime != 0 && this.actualTime == this.maxTime) {
				testEnd();
			}
			if(this.maxTime > 0) {
				this.timerLabel.Text = ""+(this.maxTime-this.actualTime)+"s";
			}
		}
	}
}
